package com.example.movie.ui.video

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.dueeeke.videoplayer.player.VideoView
import com.dueeeke.videoplayer.player.VideoViewManager
import kotlinx.android.synthetic.main.fragment_video.*

/**
 * 页面以及播放器共有逻辑封装
 */
open class BaseVideoFragment<T : VideoView<*>?> : Fragment() {

    protected var mVideoView: T? = null
    open val layoutResId: Int
        get() = 0
    private val contentView: View?
        get() = null

    private fun enableBack(): Boolean {
        return true
    }

    protected val videoViewManager: VideoViewManager
        get() = VideoViewManager.instance()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (layoutResId != 0) {
            return inflater.inflate(layoutResId,container,false)
        }
        return null
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView()
    }

    protected open fun initView() {
        btn.setOnClickListener {
            Toast.makeText(context, "点击了搜索按钮", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onResume() {
        super.onResume()
        if (mVideoView != null) {
            mVideoView!!.resume()
        }
    }

    override fun onPause() {
        super.onPause()
        mVideoView?.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mVideoView?.release()
    }

}